package org.codefinger.dao.impl.sql;

import org.codefinger.dao.impl.SQLVisitor;
import org.codefinger.dao.util.Lang;

public class JoinItem {

	private String		preffix;

	private Expression	exp;

	public JoinItem(boolean isInner, String tableName, String tableAlias, Expression exp) {
		super();
		String preffix = isInner ? " INNER JOIN " : " LEFT JOIN ";
		this.preffix = Lang.joinStr(preffix.length() + tableName.length() + tableAlias.length() + 5, preffix, tableName, " ", tableAlias, " ON ");
		this.exp = exp;
	}

	public JoinItem(boolean isInner, String tableName, Expression exp) {
		super();
		String preffix = isInner ? " INNER JOIN " : " LEFT JOIN ";
		this.preffix = Lang.joinStr(preffix.length() + tableName.length() + 4, preffix, tableName, " ON ");
		this.exp = exp;
	}

	public void visit(SQLVisitor visitor) {
		visitor.appendSQL(preffix);
		if (!exp.matchVisit(visitor)) {
			visitor.appendSQL("1 = 1");
		}
	}

}
